package Control;


import java.util.ArrayList;

import DB.*;
import Model.*;

public class Ctr {
	public Ctr()
	{
		
	}
	/* START OF Booking CTR*/
	public ArrayList<Booking> findAllBooking()
    {
      IFDBBooking dbb = new DBBooking();
      ArrayList<Booking> allb = new ArrayList<Booking>();
      allb = dbb.miscWhere();
      return allb;
    }
	public ArrayList<Booking> findAllBookingByDate(String date)
    {
      IFDBBooking dbb = new DBBooking();
      ArrayList<Booking> allb = new ArrayList<Booking>();
      allb = dbb.findAllbyDate(date);
      return allb;
    }
	public Booking findBybID(int bID)
    {
        IFDBBooking dbb = new DBBooking();
        return dbb.findBooking(bID);
    }
	public Booking findByBookingDate(String date)
    {
        IFDBBooking dbb = new DBBooking();
        return dbb.findBookingByDate(date);
    }
	public boolean updateBooking(int bID, int choice,String s,int i )
    {	boolean r=false;
		int x = 0;
        IFDBBooking dbb = new DBBooking();
        Booking b = findBybID(bID);
        if(s.isEmpty()==true)
        	switch(choice)
        	{case 0:
        		b.setCoID(i);
        		x=0;
        		break;
        	case 1:
        		b.setcID(i);
        		x=2;
        		break;
        	case 2:
        		b.setchID(i);
        		x=3;
        	break;}
        else
        	switch(choice)
        	{
        	case 0:
        		b.setDate_time(s);
        		x=1;
        		break;
        	case 1:
        		b.setsStart(s);
        		x=4;
        		break;
        	case 2:
        		b.setsEnd(s);
        		x=5;
        		break;
        	}
        	r=dbb.updateBooking(b,x);
        	return r;
    }
	
     public void updateBookingAll(int bID)
     { IFDBBooking dbb = new DBBooking();
     Booking b = findBybID(bID);
     dbb.updateBooking(b, 4);
    	 
     }
     
    
	public int insertNewBooking(int coID,String date_time,int cID,int chID, String sStart,String sEnd,int iID)
    {    int bID = -1;

         try{
          DBCon.startTransaction();
          IFDBBooking dbb = new DBBooking();
         bID= dbb.createBooking(coID, date_time, cID, chID, sStart, sEnd, iID);
          DBCon.commit();
         }
         catch(Exception e)
         {
             DBCon.rollback();
         }
         return bID;
    }
	public boolean deleteBooking(int bID)
	{ IFDBBooking db = new DBBooking();
	   return db.deleteBooking(bID);
		}
	
	public Booking findBookingbyDateTime(String date,String sStart,String sEnd)
	{ 	IFDBBooking db = new DBBooking();
		Booking bObj = db.findBookingByDateTime(date, sStart, sEnd);
		
		return bObj;
	}
	/* END OF Booking CTR*/
	

	/* START OF Client CTR*/
	public ArrayList<Client> findAllClient()
    {
      IFDBClient dbc = new DBClient();
      ArrayList<Client> allb = new ArrayList<Client>();
      allb = dbc.miscWhere();
      return allb;
    }
	public Client findBycID(int cID)
    {
        IFDBClient dbc = new DBClient();
        return dbc.findClient(cID);
    }
	public Client findBySSN(String ssn)
    {
        IFDBClient dbc = new DBClient();
        return dbc.findClientBySSN(ssn);
    }
	public boolean updateClient(int cID, int choice,String s,int i )
    {	int x = 0;
    	boolean r = false;
        IFDBClient dbc = new DBClient();
        Client c = findBycID(cID);
        if(s.isEmpty()==true)
        	switch(choice)
        	{case 0: //Update Phone
        		c.setPhone(i);
        		x=4;
        		break;
        	case 1:// Update Subscription ID
        		c.setmID(i);
        		x=5;
        		break;
        
        	case 2: // Update Session
        		c.setSession(i);
        		x=6;
        		break;
        case 4: //Add sesions	
        	int b =c.getSession() + i; 
        	c.setSession(b);
        	x=6;
        	break;
        case 5: //Substract Session
        	if(c.getSession()==0){
        		break;
        	}
        	else{
        	int f = c.getSession() - i;
        	c.setSession(f);
        	x=6;}
        	break;
        	}
        else
        { switch(choice){
        	case 0://Update FName
        		c.setFname(s);
        		x=1;
        		break;
        	case 1://UpdateLname
        		c.setLname(s);
        		x=2;
        		break;
        	case 2://SSN
        		c.setSsn(s);
        		x=0;
        		break;
        	case 3://Address
        		c.setAddress(s);
        		x=3;
        		break;
        }
        }
        	        	
        	r=dbc.updateClient(c,x);
        	return r;
    }
    /* public void updateClientAll(int cID)
     { IFDBClient dbc = new DBClient();
     Client b = findBycID(cID);
     dbc.updateClient(b, 7);
    	 
     }
    */
	public int insertNewClient(String ssn,String fname,String lname,String address,int phone,int mID, int chID)
    {    int cID=-1;

         try{
          DBCon.startTransaction();
          IFDBClient dbc = new DBClient();
          cID = dbc.createClient(ssn, fname, lname, address, phone, mID, chID);
          DBCon.commit();
         }
         catch(Exception e)
         {
             DBCon.rollback();
         }
		return cID;
    }
	public boolean deleteClient(int cID)
	{ IFDBClient db = new DBClient();
	   return db.deleteClient(cID);
		}

	/* END OF Client CTR*/
	

	/* START OF Coach CTR*/
	public ArrayList<Coach> findAllCoach()
    {
      IFDBCoach dbc = new DBCoach();
      ArrayList<Coach> allb = new ArrayList<Coach>();
      allb = dbc.miscWhere();
      return allb;
    }
	public Coach findBychID(int chID)
    {
        IFDBCoach dbc = new DBCoach();
        return dbc.findCoach(chID);
    }
	public void updateCoach(int chID, int choice,String s,int i )
    {	int x = 0;
        IFDBCoach dbc = new DBCoach();
        Coach ch = findBychID(chID);
        if(s.isEmpty()==true)
        {
        		ch.setPhone(i);
        		x=3;
        }	
        else
        	switch(choice){
        	case 0://Update Name
        		ch.setName(s);
        		x=0;
        		break;
        	case 1://Update Address
        		ch.setAddress(s);
        		x=1;
        		break;
        	case 2://SSN
        		ch.setSsn(s);
        		x=2;
        		break;
        }
        	
        	
        	dbc.updateCoach(ch,x);
    }
     public void updateCoachAll(int chID)
     { IFDBCoach dbc = new DBCoach();
     Coach b = findBychID(chID);
     dbc.updateCoach(b, 4);
    	 
     }
    
	public void insertNewCoach(String name,String address,String ssn, int phone)
    {    

         try{
          DBCon.startTransaction();
          IFDBCoach dbc = new DBCoach();
          dbc.createCoach(name, address, ssn, phone);
          DBCon.commit();
         }
         catch(Exception e)
         {
             DBCon.rollback();
         }
    }
	public void deleteCoach(int chID)
	{ IFDBCoach db = new DBCoach();
	   db.deleteCoach(chID);
		}
	/* END OF Coach CTR*/


	/* START OF Court CTR*/
	public ArrayList<Court> findAllCourt()
    {
      IFDBCourt dbc = new DBCourt();
      ArrayList<Court> allb = new ArrayList<Court>();
      allb = dbc.miscWhere();
      return allb;
    }
	public Court findBycoID(int coID)
    {
        IFDBCourt dbc = new DBCourt();
        return dbc.findCourt(coID);
    }
	public void updateCourt(int coID, int choice,String s )
    {
        IFDBCourt dbc = new DBCourt();
        Court b = findBycoID(coID);
        
        	dbc.updateCourt(b);
    }

    
	public void insertNewCourt(String co_name)
    {    

         try{
          DBCon.startTransaction();
          IFDBCourt dbc = new DBCourt();
          dbc.createCourt(co_name);
          DBCon.commit();
         }
         catch(Exception e)
         {
             DBCon.rollback();
         }
    }
	public void deleteCourt(int coID)
	{  IFDBCourt db = new DBCourt();
	   db.deleteCourt(coID);
		}
	/* END OF Court CTR*/


	/* START OF Invoice CTR*/
	public ArrayList<Invoice> findAllInvoice()
    {
      IFDBInvoice dbi = new DBInvoice();
      ArrayList<Invoice> allb = new ArrayList<Invoice>();
      allb = dbi.miscWhere();
      return allb;
    }
	public Invoice findByiID(int iID)
    {
        IFDBInvoice dbi = new DBInvoice();
        return dbi.findInvoice(iID);
    }
	public void updateInvoice(int iID, int choice,String s,int i )
    {	int x = 0;
        IFDBInvoice dbi = new DBInvoice();
        Invoice b = findByiID(iID);
        if(s.isEmpty()==true)
        	switch(choice)
        	{case 0: //Update Client
        		b.setcID(i);
        		x=1;
        		break;
        	case 1: // Update Court
        		b.setiID(i);
        		x=2;
        		break;
        	case 2: // Update Coach
        		b.setiID(i);
        		x=3;
        		break;
        	
          	
          	case 4:
          		b.setPrice(i);
          		x=5;
          		break;}
        else	
        	b.setDate(s);
        	
        	dbi.updateInvoice(b,x);
    }
     public void updateInvoiceAll(int iID)
     { IFDBInvoice dbi = new DBInvoice();
     Invoice b = findByiID(iID);
     dbi.updateInvoice(b, 6);
    	 
     }
    
	public int insertNewInvoice(String date,int cID ,int coID,int chID, int price)
    {    int iID=-1;
		
         try{
          DBCon.startTransaction();
          IFDBInvoice dbi = new DBInvoice();
          iID =dbi.createInvoice(date, cID, coID, chID, price);
          DBCon.commit();
         }
         catch(Exception e)
         {
             DBCon.rollback();
         }
         return iID;
    }
	public void deleteInvoice(int iID)
	{  IFDBInvoice db = new DBInvoice();
	   db.deleteInvoice(iID);
		}
	/* END OF Invoice CTR*/
	
	/* START OF Subscription CTR*/
	public ArrayList<Subscription> findAllSubscription()
    {
      IFDBSubscription dbs = new DBSubscription();
      ArrayList<Subscription> allb = new ArrayList<Subscription>();
      allb = dbs.miscWhere();
      return allb;
    }
	public Subscription findBymID(int mID)
    {
        IFDBSubscription dbs = new DBSubscription();
        return dbs.findSubscription(mID);
    }
	public void updateSubscription(int mID, int choice,String s,int i )
    {	int x = 0;
        IFDBSubscription dbs = new DBSubscription();
        Subscription b = findBymID(mID);
        if(i!=-1)
        	switch(choice)
        	{case 0: //Update Price
        		b.setPrice(i);
        		x=1;
        		break;
        	case 1: // Update Discount
        		b.setDiscount(i);
        		x=2;
        		break;

          }
        else	
        	b.setType(s);
        	
        	dbs.updateSubscription(b,x);
    }
     public void updateSubscriptionAll(int mID)
     { IFDBSubscription dbs = new DBSubscription();
     Subscription b = findBymID(mID);
     dbs.updateSubscription(b, 6);
    	 
     }
    
	public void insertNewSubscription(String type,int price, int discount)
    {    

         try{
          DBCon.startTransaction();
          IFDBSubscription dbs = new DBSubscription();
          dbs.createSubscription(type, price, discount);
          DBCon.commit();
         }
         catch(Exception e)
         {
             DBCon.rollback();
         }
    }
	public void deleteSubscription(int mID)
	{  IFDBSubscription db = new DBSubscription();
	   db.deleteSubscription(mID);
		}
	/* END OF Subscription CTR*/
}
